package com.hei.kml;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.hei.kml.schame.Template;
import com.hei.kml.schame.Waylines;

import java.io.File;
import java.nio.charset.Charset;

public class Serializer {

	public static File createKmzFile(String waylinesDocument, File target) {
		// 创建临时文件夹wpmz，并生成子文件template.kml waylines.kml
		String wpmz = FileUtil.getTmpDirPath() + "/" + System.currentTimeMillis() + "/wpmz";

		if (StrUtil.isNotBlank(waylinesDocument)) {
			String template = new Template().fromJSON(waylinesDocument).toKML();
			FileUtil.writeString(template, FileUtil.file(wpmz + "/template.kml"), Charset.defaultCharset());
		}

		if (StrUtil.isNotBlank(waylinesDocument)) {
			String waylines = new Waylines().fromJSON(waylinesDocument).toKML();
			FileUtil.writeString(waylines, FileUtil.file(wpmz + "/waylines.wpml"), Charset.defaultCharset());
		}

		// 压缩临时文件夹wpmz
		ZipUtil.zip(target,true, FileUtil.file(wpmz));

		// 删除临时文件夹wpmz
		FileUtil.del(wpmz);
		return target;
	}

}
